package tt.dz.appserver.service.business.imp;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.map.ListOrderedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;

import tt.dz.appserver.dao.base.BasisDao;
import tt.dz.appserver.entity.model.Tb_user;
import tt.dz.appserver.service.business.UserService;
import tt.dz.appserver.util.PageModel;

@SuppressWarnings("rawtypes")
public class UserServiceImp implements UserService {

	@Autowired
	private BasisDao<Tb_user> basisDao;

	@Cacheable(value = "user")
	public Collection<Map<String, Object>> userList(int pageNo, int pageSize) {
		Collection<Map<String, Object>> collection = basisDao.findDataOfMapBySQL(
				new StringBuffer("SELECT e.`Name`,e.monthly_salary,e.`month`,e.dept_no,u.id,u.username,"
						+ "u.pwd,u.roleid FROM employee AS e LEFT JOIN users AS u ON e.ID = u.roleid"),
				pageNo, pageSize);
		return collection;
	}

	@Cacheable(value = "user", key = "#id")
	public Tb_user getUserById(String id) {
		ListOrderedMap paramsMap = new ListOrderedMap();
		paramsMap.put("id", id);
		List<Tb_user> list = basisDao.findListAutomaticOfClass(Tb_user.class, paramsMap, null, null, "id", 0);
		if (list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	@Cacheable(value = "user", key = "#user.id")
	public PageModel getUser(Tb_user user) {
		return basisDao.pageingQueryExample(user, 1, 20, "valid_flag", 1);
	}

	// @CacheEvict(key="#user.id")
	public int deleteUser(Tb_user user) {
		return basisDao.LogicDelete(user);
	}

	// @CacheEvict(key="#user.id")
	public int updateUser(Tb_user user) {
		return basisDao.update(user);
	}
}
